package com.nobuyme.box.toy;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.nobuyme.box.MainActivity;
import com.nobuyme.box.R;
import com.nobuyme.box.account.Account;
import com.nobuyme.box.account.AccountManager;
import com.nobuyme.box.toy.manage.PutGoodsActivity;
import com.nobuyme.box.xunfei.XunFeiSpeech;
import com.pachong.android.baseuicomponent.activity.BaseActivity;

import butterknife.Bind;
import butterknife.OnClick;

public class SuccessActivity extends BaseActivity {
    @Bind(R.id.tv_success_num)
    TextView mTvSuccessNum;
    private MyCountDownTimer mc;
    private final static String POSITION = "openPosition";
    @Bind(R.id.btn_success_back)
    Button mBtnSuccessBack;
    private int openPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideToolbar();
        openPosition = getIntent().getExtras().getInt(POSITION);
        mc = new MyCountDownTimer(60000, 1000);
        mc.start();
        initData();
    }

    private void initData() {
        mTvSuccessNum.setText(openPosition+"号储物格已打开");
        Account acc = AccountManager.getCurrentAccount();
        // //租赁成功的时候第二次开柜
        PutGoodsActivity.openLattice(acc.getId(),acc.getPolling_one(),acc.getPolling_two(),acc.getPolling_three(),acc.getPolling_four(),openPosition);
        //语音
        try {
            XunFeiSpeech xunFeiSpeech = new XunFeiSpeech(getBaseContext());
            xunFeiSpeech.speak("12", openPosition + "号储物格已打开,操作完成请关柜门", new XunFeiSpeech.OnCompleteListener() {
                @Override
                public void onComplete() {
                }
            });
        } catch (NullPointerException e) {

        }
    }

    public static void start(Activity activity,int openPosition) {
        Intent intent = new Intent(activity, SuccessActivity.class);
        intent.putExtra(POSITION, openPosition);
        activity.startActivity(intent);
    }

    public static void start(Context context, int openPosition) {
        Intent intent = new Intent(context, SuccessActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        intent.putExtra(POSITION, openPosition);
        context.startActivity(intent);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_success, null);
    }

    @OnClick({R.id.btn_success_back,R.id.btn_open_again})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_success_back:
                if (mc != null) {
                    mc.cancel();
                }
                //退出时要变更状态
                MainActivity.start(SuccessActivity.this);
                finish();
                break;

            case R.id.btn_open_again:
                // //租赁成功没开柜，可点击开柜
                Account acc = AccountManager.getCurrentAccount();
                PutGoodsActivity.openLattice(acc.getId(),acc.getPolling_one(),acc.getPolling_two(),acc.getPolling_three(),acc.getPolling_four(),openPosition);
                break;

        }

    }



    /**
     * 继承 CountDownTimer 防范
     * <p>
     * 重写 父类的方法 onTick() 、 onFinish()
     */
    class MyCountDownTimer extends CountDownTimer {
        /**
         * @param millisInFuture    表示以毫秒为单位 倒计时的总数
         *                          <p>
         *                          例如 millisInFuture=1000 表示1秒
         * @param countDownInterval 表示 间隔 多少微秒 调用一次 onTick 方法
         *                          <p>
         *                          例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()
         */
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            if (mc != null) {
                mc.cancel();
            }
            MainActivity.start(SuccessActivity.this);
            finish();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (mBtnSuccessBack != null) {
                mBtnSuccessBack.setText(millisUntilFinished / 1000 + "秒后返回首页");
            }

        }
    }

}
